﻿#pragma execution_character_set("utf-8")
// 本文件为utf-8 编码格式

#include "GDTaskView.h"
#include "../RootWindow.h"
#include "../Util/GDHttpUtil.h"
#include "../Util/GDUtil.h"

static GDTaskView *instance = NULL;

void GDTaskView::showTask(const std::string& msg)
{
	if (instance == NULL){
		instance = new GDTaskView();
		if (instance && instance->init()){
			instance->initView();
			RootWindow::getInstance()->insertSubview(instance,3000);
		}
		else{
			instance = NULL;
		}
	}

	if (instance){
		instance->show(msg);
	}
}

GDTaskView::GDTaskView()
:taskLabel(NULL)
, taskBgImage(NULL)
{
}


GDTaskView::~GDTaskView(void)
{
}

void GDTaskView::initView(){
	this->setColor(CAColor_clear);
	taskBgImage = CAScale9ImageView::createWithFrame(CCRect(0, 0, _ds(80), _ds(80)));
	taskBgImage->setImage(CAImage::create(GD_IMAGE_PATH + "icon_refresh_bg.png"));
	this->addSubview(taskBgImage);
	taskLabel = GDUtil::createLabelWithFrame(CCRect(_ds(20),_ds(10),_ds(40),_ds(40)), "", CAColor_white, _ds(20), "", CATextAlignmentCenter, CAVerticalTextAlignmentCenter);
	this->insertSubview(taskLabel, 2);
	this->setAlpha(0);
}

void GDTaskView::show(const std::string& msg){
	CCSize winSize = RootWindow::getInstance()->getBounds().size;
	taskLabel->setText(msg);
	float width = GDUtil::getLabelWidth(taskLabel);
	float height = _ds(50);
	float maxWidth = winSize.width-_ds(90);
	float maxHeight = _ds(200);

	if (width > maxWidth){ 
		width = maxWidth;
	}
	width += _ds(10);
	GDUtil::setViewWidth(taskLabel, width);

	height = GDUtil::getLabelHeight(taskLabel);
	if (height > maxHeight){
		height = maxHeight;
	}

	GDUtil::setViewHeight(taskLabel, height);
	taskBgImage->setFrame(CCRect(0,0,width + _ds(40), height + _ds(20)));
	this->setContentSize(taskBgImage->getBounds().size);

	this->setFrameOrigin(CCPoint((winSize.width - this->getBounds().size.width) / 2, (winSize.height - this->getBounds().size.height)/2));


	this->setVisible(true);
	this->runAction(CCSequence::create(CCFadeIn::create(0.4), CCDelayTime::create(3), CCFadeOut::create(1),NULL));
	if (!CAScheduler::isScheduled(schedule_selector(GDTaskView::closeView), this)){
		CAScheduler::schedule(schedule_selector(GDTaskView::closeView), this, 0, 0, 5);
	}
}

void GDTaskView::closeView(float dt)
{
	this->setVisible(false);
	/*
	if (CAScheduler::isScheduled(schedule_selector(GDTaskView::closeView), this)){
		CAScheduler::unschedule(schedule_selector(GDTaskView::closeView), this);
	}
	*/
}